package org.subside.MQ.rabbitmq.WorkQueue;

import com.rabbitmq.client.*;
import org.subside.MQ.rabbitmq.config.ConnectionUtil;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Author : zhangbo
 * @data : 2024/4/12
 * @description :一条消息只能被一个消费者获取
 */
public class Publisher {
    /**
     * 队列的名称
     */
    private static final String QUEUE_NAME = "workQueue";

    public static void main(String[] args) throws Exception {
        Connection connection =  ConnectionUtil.getConnection();
        if(connection !=null){
            Channel channel = null;
            try {
                //建立信道
                channel = connection.createChannel();
                for(int i=0; i<50; i++){
                    String msg = "第"+ (i+1) +"条消息";
                    channel.basicPublish("", QUEUE_NAME, null, msg.getBytes("UTF-8"));
                    System.out.println("生产者发送:"+msg);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    channel.close();
                    connection.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (TimeoutException e) {
                    e.printStackTrace();
                }
            }

        }
    }

}
